package com.threeonemeeting.util;

import java.io.BufferedWriter;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileWriter;
import java.io.IOException;

import android.content.Context;
import android.util.Log;

import com.snda.lib.util.FileHelper;
import com.snda.lib.util.MD5Helper;
import com.snda.lib.util.MiscHelper;
import com.threeonemeeting.Const;

/**
 * 文件缓存，接收到的协议内容保存到文件中
 * 
 * @author Administrator
 * 
 */
public class PathUtil {

	private static String getSavePath(Context ctx, String strUrl) {

		String strSavePath = AppSettings.getResponsePath(ctx);

		if (isPagedUrl(strUrl) == true) {

			// 检查strUrl中是否包含page参数
			String strResultUrl = "";
			int nBeginIndex = strUrl.indexOf("page=");
			if (nBeginIndex != -1) {

				// 找到page=1后面的&符号
				String strEnd = strUrl.substring(nBeginIndex);
				int nEndIndex = strEnd.indexOf("&");
				if (nEndIndex == -1) {
					strResultUrl = strUrl.substring(0, nBeginIndex);
				} else {
					String strBefore = strUrl.substring(0, nBeginIndex);
					String strAfter = strEnd.substring(nEndIndex);
					strResultUrl = strBefore + strAfter;
				}
			} else {
				Log.d(Const.Tag, "no page find in Url");
			}

			String strDirPath = MD5Helper.getStringMD5(strResultUrl);
			strSavePath += strDirPath;
		} else {
			strSavePath += MD5Helper.getStringMD5(strUrl);
		}
		strSavePath += "/";

		return strSavePath;
	}

	/**
	 * @desc 检查URL中是否包含page=xxx
	 * @param strUrl
	 * @return
	 */
	public static boolean isPagedUrl(String strUrl) {
		int nBeginIndex = strUrl.indexOf("page=");
		if (nBeginIndex != -1) {
			return true;
		}

		return false;
	}

	// 存储指定URL的响应数据
	public static boolean saveToCache(Context ctx, String strUrl,
			String strResponse) {

		String strSavePath = getSavePath(ctx, strUrl);
		File path = new File(strSavePath);
		if (path.exists() == false) {
			path.getParentFile().mkdirs();
		}
		path.mkdir();

		String strMD5Url = MD5Helper.getStringMD5(strUrl);
		strSavePath = strSavePath + strMD5Url + ".txt";
		try {
			BufferedWriter out = new BufferedWriter(new FileWriter(strSavePath,
					false));
			out.write(strResponse);
			out.close();
		} catch (IOException e) {
			return false;
		}

		return true;
	}

	public static String getFromCache(Context ctx, String strUrl) {

		String strSavePath = getSavePath(ctx, strUrl);
		String strMD5Url = MD5Helper.getStringMD5(strUrl);
		strSavePath = strSavePath + strMD5Url + ".txt";
		File file = new File(strSavePath);
		if (file.exists() == false) {
			return null;
		}

		try {
			FileInputStream fis = new FileInputStream(strSavePath);
			String strResponse = MiscHelper.inputStream2String(fis);
			fis.close();
			return strResponse;

		} catch (IOException e) {
			return null;
		}
	}

	public static void removeCache(Context ctx, String strUrl) {
		String strSavePath = getSavePath(ctx, strUrl);
		FileHelper.delFolder(strSavePath);
	}

}
